package com.zyk.leetcode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author zhangsan
 * @date 2021/4/12 15:29
 */
public class M0807 {

    List<String> record = new ArrayList<>();

    // https://leetcode-cn.com/problems/permutation-i-lcci/
    public String[] permutation(String S) {
        process(S.toCharArray(), 0);
        String[] ans = new String[record.size()];
        return record.toArray(ans);
    }

    public void process(char[] str, int i) {
        if (i == str.length - 1) {
            record.add(new String(str));
        }
        for (int j = i; j < str.length; j++) {
            swap(str, i, j);
            process(str, i + 1);
            swap(str, i, j);
        }
    }

    public void swap(char[] str, int i, int j) {
        char temp = str[i];
        str[i] = str[j];
        str[j] = temp;
    }

    // for test
    public static void main(String[] args) {
        M0807 test = new M0807();
        String str = "qwe";
        String[] ans = test.permutation(str);
        System.out.println(Arrays.toString(ans));
    }

}
